package com.china08.yxyapi.api.manage.sch.platform.resource.yxylectureprepack;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lecture.YxyLectureFeedBack;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLectureFeedBackRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;
import com.china08.yxyapi.service.db.YxyLecturePrePackService;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLectureSchDelPrePackFeedBackController extends YxyApiBaseController {

    Logger log = Logger.getLogger(YxyLectureSchPrePackStatusController.class);

    @Autowired
    YxyLectureFeedBackRepository yxyLectureFeedBackRepository;

    @Autowired
    YxyLecturePrePackService yxyLecturePrePackServiceImpl;

    @Autowired
    YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @ApiOperation(value = "忽略举报备课包")
    @RequestMapping(value = "/api_yw/manage/sch/platform/resource/lectureprepack/feedback/del", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String prepackId = reqModel.prepackId;

        // 备课包上架
        YxyLecturePrePack yxyLecturePrePack = yxyLecturePrePackRepository.findById(prepackId);
        if (null != yxyLecturePrePack) {
            yxyLecturePrePack.setStatus(1);
            yxyLecturePrePackRepository.save(yxyLecturePrePack);
        }

        // 删除举报信息
        List<YxyLectureFeedBack> list = yxyLectureFeedBackRepository.findByPrepackIdOrderByCreatedDateDesc(prepackId);
        if (list.size() > 0) {
            yxyLectureFeedBackRepository.delete(list);
        }
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String prepackId;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(prepackId), "");
        }
    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
